package com.zz.framework.core.ext

import com.zz.framework.R
import java.text.SimpleDateFormat
import java.util.*

/**
 * Copyright © 2019 zanezhao All rights reserved.
 * Created by zanezhao on 2019-12-01 11:20.
 * @author: zanezhao 1820022519@qq.com
 * @version: V1.0
 */

/**
*  字符串日期格式（比如：2018-4-6)转为毫秒
*  @param format 时间的格式，默认是按照yyyy-MM-dd HH:mm:ss来转换，如果您的格式不一样，则需要传入对应的格式
*/
fun String.toDateMills(format: String = timeFormatStringYMDHMS) = SimpleDateFormat(format, Locale.getDefault()).parse(this).time

/**
 * Long类型时间戳转为字符串的日期格式
 * @param format 时间的格式，默认是按照yyyy-MM-dd HH:mm:ss来转换，如果您的格式不一样，则需要传入对应的格式
 */
fun Long.toDateString(format: String = timeFormatStringYMDHMS) = SimpleDateFormat(format, Locale.getDefault()).format(
    Date(this)
)

fun Int.toDateString(format: String = timeFormatStringYMDHMS) = SimpleDateFormat(format, Locale.getDefault()).format(
    Date(this.toLong())
)
fun Int.toUnixDateString(format: String = timeFormatStringYMDHMS) = SimpleDateFormat(format, Locale.getDefault()).format(
    Date(this.toLong()*1000)
)

fun Long.toMsgVoiceTime():String{
    val ss = 1000
//    val mi = ss * 60
//    val hh = mi * 60
//    val dd = hh * 24
//    val day: Long = this / dd
//    val hour: Long = (this - day * dd) / hh
//    val minute: Long = (this - day * dd - hour * hh) / mi
//    val second: Long = (this - day * dd - hour * hh - minute * mi) / ss
    val sb = StringBuffer()
//    if (day > 0) {
//        sb.append(day.toString() + "d")
//    }
//    if (hour > 0) {
//        sb.append(hour.toString() + "h")
//    }
//    if (minute > 0) {
//        sb.append("$minute′")
//    }
//    if (second > 0) {
//        sb.append("$second″")
//    }
    val second: Long = (this) / ss
    sb.append("$second″")
    return sb.toString()
}

//距离大于一年：21-07-06
//大于等于24小时小于1年：07-06
//小于24小时，大于等于1小时：xx小时前
//5分钟~60分：xx分钟前
//5分钟以下：刚刚
fun Long.toChatTime():String{
//    val ss = 1000
//    val mi = ss * 60
//    val hh = mi * 60
//    val dd = hh * 24
//    val day: Long = this / dd
//    val hour: Long = (this - day * dd) / hh
//    val minute: Long = (this - day * dd - hour * hh) / mi
//    val second: Long = (this - day * dd - hour * hh - minute * mi) / ss
    val sb = StringBuffer()
//    if (day > 0) {
//        return times(1000)?.toDateString(timeFormatStringYMD)
//    }else if(hour>=24){
//        times(1000)?.toDateString(timeFormatStringMD)
//    }
//
//    if (hour > 0) {
//        sb.append(hour.toString() + "h")
//    }
//    if (minute > 0) {
//        sb.append("$minute′")
//    }
//    if (second > 0) {
//        sb.append("$second″")
//    }
    return sb.toString()
}

val Any.timeFormatStringYMDHMS: String
    get() = "yyyy-MM-dd HH:mm:ss"
val Any.timeFormatStringMDHM: String
    get() = "MM-dd HH:mm"
val Any.timeFormatStringYMDHM: String
    get() = "yyyy-MM-dd HH:mm"
val Any.timeFormatStringYMDHM2: String
    get() = getString(R.string.text_format_ymdhm)
val Any.timeFormatStringYMDHM3: String
    get() = "yyyy/MM/dd HH:mm"
val Any.timeFormatStringYMD: String
    get() = "yyyy-MM-dd"
val Any.timeFormatStringHMS: String
    get() = "HH:mm:ss"
val Any.timeFormatStringYM: String
    get() = "yyyy-MM"
val Any.timeFormatStringMD: String
    get() = "MM-dd"

